package com.java.prttern.notes;

import com.java.prttern.Facotry.Rectangle;
import com.java.prttern.Facotry.Shape;
import com.java.prttern.Facotry.Square;

public class ShapeFactory {
    //使用反射机制可以解决每次增加一个产品时，都需要增加一个对象实现工厂的缺点
    public static Object getClass(Class<?extends Shape> clazz){
        Object obj = null;
        try {
            obj = Class.forName(clazz.getName()).newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return obj;
    }

    public static void main(String[] args) {
        //采用强制转换，这样就只需要一个对象实现工厂
        Rectangle rect= (Rectangle) ShapeFactory.getClass(Rectangle.class);
        rect.draw();
        Square square= (Square) ShapeFactory.getClass(Square.class);
        square.draw();
    }
}
